﻿using System.Runtime.CompilerServices;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using WssEvents;
using DG.Tweening;
using UnityEngine.EventSystems;

/// <summary>
/// 支持摄像机环绕着观察某个物体(锚点)，可以限制观察视角的范围
/// </summary>
public class CameraSurround : MonoBehaviour
{
    public float limitTop = 7.3f;
    public float limitBottom = 0;

    public Camera mCamera;

    public Vector3 targetEulerAngles = new Vector3();

    public bool enableInput = true;

    [Tooltip("是否对点击到了UI元素而进行block")]
    public bool isUIBlock = false;

    [Header("===== 相机移动缩放速度 =====")]
    public float dragSpeed = 1.0f;
    public float scrollSpeed = 1.0f;

    public float cameraScaler = 1.0f;

    [Header("===== 控制开关 =====")]
    public bool enableScroll = true;
    public bool enableDragV = true;
    public bool enableDragH = true;


    //缓存初始化信息

    private Vector3 _lastMousePosition = new Vector3(-1, -1, -1);

    private Vector3 _defaultMousePos = new Vector3(-1, -1, -1);

    // private Vector3 _defaultCameraPos = new Vector3();

    private float _originCameraFov = 0f;
    private float _originCameraSize = 0f;



    private void Awake()
    {
        targetEulerAngles = this.transform.eulerAngles;
        mCamera = this.GetComponentInChildren<Camera>();
        _originCameraFov = this.mCamera.fieldOfView;
        _originCameraSize = this.mCamera.orthographicSize;
    }

    public void ResetCamera()
    {
        this.SetCameraState(Vector3.zero, 1.0f);
    }

    public void SetCameraState(Vector3 angles, float scaler)
    {
        this.cameraScaler = scaler;
        this.targetEulerAngles = angles;

    }

    // Update is called once per frame
    void Update()
    {
        if (!enableInput) return;
        if (this.isUIBlock)
        {
            if (this.IsClickUI()) return;
        }

        //鼠标滑动操作
        if (Input.GetMouseButton(0))
        {
            Vector3 delta = Input.mousePosition - _lastMousePosition;
            if (enableDragV == false) delta.y = 0;
            if (enableDragH == false) delta.x = 0;

            if (!_lastMousePosition.Equals(_defaultMousePos))
            {
                delta *= 0.20f;
                delta.x = Mathf.Clamp(delta.x, -15, 15);
                delta.y = Mathf.Clamp(delta.y, -15, 15);

                targetEulerAngles.y += delta.x;
                targetEulerAngles.x -= delta.y;

                if (targetEulerAngles.y > 180)
                {
                    targetEulerAngles.y -= 360;
                }
                else if (targetEulerAngles.y < -180)
                {
                    targetEulerAngles.y += 360;
                }

                targetEulerAngles.x = Mathf.Clamp(targetEulerAngles.x, limitBottom, limitTop);
            }
            _lastMousePosition = Input.mousePosition;
        }
        else
        {
            _lastMousePosition = _defaultMousePos;
        }

        //鼠标滚轮操作 (缩放)
        if (enableScroll)
        {
            const string MOUSE_SCROLL = "Mouse ScrollWheel";
            float mouseScroll = Input.GetAxis(MOUSE_SCROLL);
            if (mouseScroll != 0)
            {
                float targetScale = this.cameraScaler + mouseScroll * 14f * Time.deltaTime;
                this.cameraScaler = Mathf.Clamp(targetScale, 0, 2.0f);
            }
        }

        //鼠标中键操作 (复位)
        if (Input.GetMouseButtonDown(2))
        {
            this.ResetCamera();
        }

    }

    private void LateUpdate()
    {
        var angles = this.transform.eulerAngles;
        angles.x = Mathf.LerpAngle(angles.x, targetEulerAngles.x, Time.deltaTime * 8f);
        angles.y = Mathf.LerpAngle(angles.y, targetEulerAngles.y, Time.deltaTime * 8f);
        this.transform.eulerAngles = angles;

        //camera缩放
        if (this.mCamera)
        {
            this.mCamera.orthographicSize = Mathf.Lerp(this.mCamera.orthographicSize, _originCameraSize * this.cameraScaler, Time.deltaTime * 8f);
            this.mCamera.fieldOfView = Mathf.Lerp(this.mCamera.fieldOfView, _originCameraFov * this.cameraScaler, Time.deltaTime * 8f);
        }
    }

    /// <summary>
        /// 检测是否点击在UI上
        /// </summary>
        /// <returns></returns>
    private bool IsClickUI()
    {
        if (EventSystem.current != null)
        {
            PointerEventData eventData = new PointerEventData(EventSystem.current);
            eventData.position = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
            List<RaycastResult> results = new List<RaycastResult>();
            EventSystem.current.RaycastAll(eventData, results);
            return results.Count > 0;
        }
        return false;

    }

}
